# -*- coding: utf-8 -*-
# owner: shenyi

from typing import Dict, Type, Any
from src.providers.base import BaseProvider

# 延迟导入，避免循环依赖
_PROVIDER_REGISTRY: Dict[str, Type[BaseProvider]] = {}

def register_provider(name: str):
    """装饰器：用于注册云厂商实现"""
    def decorator(cls: Type[BaseProvider]):
        _PROVIDER_REGISTRY[name] = cls
        return cls
    return decorator

def get_provider(name: str, **config) -> BaseProvider:
    """
    工厂方法：根据名称创建云厂商实例

    :param name: 云厂商标识，如 'aliyun', 'tencent', 'huawei', 'aws', 'google'
    :param config: 认证信息等（如 access_key, secret_key）
    :return: 对应的 Provider 实例
    """
    if name not in _PROVIDER_REGISTRY:
        valid = ", ".join(_PROVIDER_REGISTRY.keys())
        raise ValueError(f"Unsupported cloud provider: '{name}'. Valid options: {valid}")
    
    provider_class = _PROVIDER_REGISTRY[name]
    return provider_class(**config)

# 手动注册（或通过装饰器自动注册）
from .aliyun.ecs import Ecs as AliyunEcs
from .tencent.cvm import Cvm as TencentCvm
from .huawei.ecs import Ecs as HuaweiEcs
from .aws.ec2 import Ec2 as AwsEc2
from .google.compute import Compute as GoogleCompute

_PROVIDER_REGISTRY.update({
    "aliyun": AliyunEcs,
    "tencent": TencentCvm,
    "huawei": HuaweiEcs,
    "aws": AwsEc2,
    "google": GoogleCompute,
})